package com.test.dao.impl;

import com.test.dao.DeliverymanDao;
import com.test.domain.Deliveryman;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;


@Repository
public class DeliverymanDaoImpl implements DeliverymanDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insertDeliveryman(Deliveryman deliveryman) {
        String sql = "insert into deliveryman values(?,?,?,?,?)";
        jdbcTemplate.update(sql,deliveryman.getPhoneNumber(),deliveryman.getName(),deliveryman.getIdNumber(),
                                0,deliveryman.getMerchantPhoneNumber());
    }

    @Override
    public List<Deliveryman> getDeliverymanListByPhoneNumber(String phoneNumber) {
        String sql = "select * from deliveryman where merchant_phone_number=?";

        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Deliveryman>(Deliveryman.class),phoneNumber);
    }

    @Override
    public void updateIdNumber(String phoneNumber, String idNumber) {
        String sql = "update deliveryman set id_number=? where phone_number=?";
        jdbcTemplate.update(sql,idNumber,phoneNumber);
    }

    @Override
    public void updateSituation(String phoneNumber, int situation) {
        String sql = "update deliveryman set situation=? where phone_number=?";
        jdbcTemplate.update(sql,situation,phoneNumber);
    }

    @Override
    public void deleteDeliverymanByPhoneNumber(String phoneNumber) {
        String sql = "delete from deliveryman where phone_number=?";
        jdbcTemplate.update(sql,phoneNumber);
    }
}
